<?php
/**
 * @package		mod_qlform
 * @copyright	Copyright (C) 2012 ql.de All rights reserved.
 * @author 		Mareike Riegel mareike.riegel@ql.de
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;
//jimport('joomla.application.component.modelform');

require_once(dirname(__FILE__).'/helper.php');

/*Params: Filling params to variables*/
$moduleclass_sfx = htmlspecialchars($params->get('moduleclass_sfx'));
$action = htmlspecialchars($params->get('action'));
$submit = htmlspecialchars($params->get('submit'));
$messageType = $params->get('messageType');
$message=$params->get('message');
$showpretext=$params->get('showpretext');
$pretext=$params->get('pretext');
$emailcloak=$params->get('emailcloak');
$location= $params->get('location');
$showDatabaseFormError=$params->get('showDatabaseFormError');
$showCaptacha=$params->get('captcha');

$fieldModuleId=$params->get('fieldModuleId');
$moduleId=$module->id;

/*Xml: getting xml string from params*/
$obj_helper= new modQlFormHelper;
$str_xml = $obj_helper->transformText($params->get('xml'));
/*adding fields*/
$arrFields=array('user_id', 'user_email','article_id','article_title');
foreach ($arrFields as $k=>$v) if (1==$params->get($v)) $obj_helper->generateArrayField($v,"hidden");
if (1==$params->get('sendcopy')) 
{ 
	$obj_helper->generateArrayField('sendcopy',"checkbox",JText::_('MOD_QLFORM_SENDCOPY_LABEL'),1);
	$obj_helper->arrFields[]=array("name"=>'sendcopy',"value"=>1,"type"=>"checkbox","label"=>JText::_('MOD_QLFORM_SENDCOPY_LABEL'));
}
if (isset($obj_helper->arrFields) AND is_array($obj_helper->arrFields)) $str_xml = $obj_helper->addFieldsToXml($str_xml,$obj_helper->arrFields);

/*transform xml to form object*/
$form=$obj_helper->getForm($str_xml);

/*initiate captcha*/
if (1==$showCaptacha) $obj_helper->initiateCaptcha();
if (1==$params->get('todoDatabase')) 
{
	$obj_helper->connectToDatabase();
	$checkDatabase=$obj_helper->checkDatabase($params->get('databasetable'),$str_xml,$showDatabaseFormError,$params->get('databaseaddcreated'));
}

/*validation server site*/
if 
(
	(1==$fieldModuleId AND isset($_POST['moduleId']) AND $_POST['moduleId']==$moduleId AND isset($_POST['formSent']) AND 1==$_POST['formSent'] AND is_object($form))
	OR
	(0==$fieldModuleId AND isset($_POST['formSent']) AND 1==$_POST['formSent'] AND is_object($form))
)
{
	$data=$_POST['jform'];
	$validatedForm=$obj_helper->validate($data);
	if (1==$showCaptacha) $validatedCaptcha=$obj_helper->checkCaptcha($_POST['captcha'],$data);
	
	if  (1==$validatedForm AND (0==$showCaptacha OR (1==$showCaptacha AND 1==$validatedCaptcha)))
	$validated=true;
	else 
	{
		foreach ($data as $k=>$v) if (is_string($v))$data[$k]=strip_tags(html_entity_decode($v));
		$obj_helper->obj_form->fillForm($data);
		//print_r($data);
		$validated=false;
	}
}

if (1==$params->get('addPostToForm')) 
{
	$array_posts=$obj_helper->mergeSubarrays($_POST,"jform","former");
	if (isset($data) AND is_array($array_posts))$data=array_merge($data,$array_posts);
	//echo "<pre>";print_r($data);die;
}

if (isset($validated) AND 1==$validated) 
{
	if (1==$params->get('show_data_sent'))
	{
		ob_start();var_dump($data);$str_data=ob_get_contents();ob_end_clean();
		$obj_helper->arrMessages[]=array("str"=>$str_data);
	}
	if (1==$params->get('todoEmail'))
	{
		$obj_helper->mail($params->get('recipient'),$params->get('subject'),$data);
	}
	if (1==$params->get('todoDatabase') AND 1==$checkDatabase)
	{
		if (1==$params->get('databaseaddcreated')) $data['created']=date("Y-m-d H:i:s");
	 	$obj_helper->saveToDatabase($params->get('databasetable'),$data,$showDatabaseFormError);
	 }
	if (1==$params->get('todoSomethingElse'))
	{
		$obj_helper->doSomethingElse($data,$params);
	}
	if (1==$params->get('sendcopy') AND isset($_POST['sendcopy']) AND 1==$_POST['sendcopy'] AND !empty($data[$params->get('sendcopyfieldname')])) $obj_helper->mail($data[$params->get('sendcopyfieldname')],"copy: ".$params->get('subject'),$data);
	if (1==$params->get('locationbool') AND !empty($location)) header("location:".$location);
	$obj_helper->arrMessages[]=array("str"=>$message);
} 

/*Display messages*/
if (isset($obj_helper->arrMessages) AND is_array($obj_helper->arrMessages)) $messages=$obj_helper->displayMessages($messageType);

/*Captcha: initiale captcha*/
if (1==$showCaptacha) 
{
	$captcha=$obj_helper->generateCaptcha();
	$image=$obj_helper->captcha;
}

require JModuleHelper::getLayoutPath('mod_qlform', $params->get('layout', 'default'));